package com.yanqu.road.server.cmd.activity.summergames.fromcross;

import com.yanqu.road.dao.impl.activity.summergames.SummerGamesUserDataDaoImpl;
import com.yanqu.road.dao.impl.player.UserBagDaoImpl;
import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesExchangeTip;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.summergames.LogSummerGamesExchange;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_SUMMER_GAMES_EXCHANGE_DONE_REWARD_FROM_CROSS, desc = "交换完成奖励申请者(跨服返回)")
public class SummerGamesExchangeDoneRewardBackCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {
        SummerGamesProto.CrossSummerGamesExchangeDoneRewardBackMsg backMsg = SummerGamesProto.CrossSummerGamesExchangeDoneRewardBackMsg.parseFrom(yanQuMessage.getBytes());
        long playerId = backMsg.getInfo().getPlayerId();
        int wantId = backMsg.getInfo().getNeedId();
//        String name = backMsg.getInfo().getTempUserName();
//        String serverName = "";
//        ServerInfo serverInfo = ServerListMgr.getServerInfo(backMsg.getInfo().getAppointUserServerId());
//        if(serverInfo != null){
//            serverName = serverInfo.getServerName();
//        }
//        String language = "";
//        UserInfo userInfo = UserMgr.getUserInfo(playerId);
//        if(userInfo != null){
//            language = userInfo.getLanguage();
//        }
//        String myGoodsName = "";
//        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(backMsg.getInfo().getProvideId());
//        if(goodsInfo != null){
//            myGoodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
//        }
//        String otherGoodsName = "";
//        goodsInfo = GoodsMgr.getGoodsById(backMsg.getInfo().getNeedId());
//        if(goodsInfo != null){
//            otherGoodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
//        }
//        String mailTitle = ServerLanguageMgr.getContent(MailManager.SUMMER_GAMES_MAIL_TITLE_3, language);
//        String mailContent = MessageFormat.format(ServerLanguageMgr.getContent(MailManager.SUMMER_GAMES_CONTENT_3,
//                language), myGoodsName, serverName, name, otherGoodsName);
//        MailManager.sendMail(playerId, 0, eMailType.SYSTEM.getValue(), wantId + "=1", mailContent, mailTitle, new Date());
        GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(playerId);
        if(onlinePlayer == null){
            UserBagItem userBagItem = new UserBagDaoImpl().getUserBagItem(playerId, wantId);
            if(userBagItem == null){
                userBagItem = new UserBagItem();
                userBagItem.setUserId(playerId);
                userBagItem.setGoodsId(wantId);
                userBagItem.setInsertOption();
            }
            userBagItem.setNum(userBagItem.getNum() + 1);
            userBagItem.setModifyTime(System.currentTimeMillis() / 1000);

            long vipExp = 0;
            UserInfo userInfo = UserMgr.getUserInfoInRAM(playerId);
            if(userInfo != null){
                vipExp = userInfo.getVipExp();
            }

            LogMgr.addLogGoods(userBagItem.getGoodsId(), playerId, true, 1, userBagItem.getNum(), eLogMoneyType.SummerGamesActivityExchangeReward, vipExp);

            if(userBagItem.isInsertOption()){
                new UserBagDaoImpl().add(userBagItem);
            }else {
                new UserBagDaoImpl().update(userBagItem);
            }
            SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
            if(config != null) {
                SummerGamesUserData userData = new SummerGamesUserDataDaoImpl().getSummerGamesUserData(config.getActivityId(), playerId);
                if(userData != null){
                    SummerGamesExchangeTip tip = new SummerGamesExchangeTip(backMsg.getInfo().getId(), backMsg.getInfo().getNeedId(), 1);
                    userData.getExchangeTipMap().put(tip.getId(), tip);
                    new SummerGamesUserDataDaoImpl().update(userData);
                }
            }
        }else {
            SummerGamesModule gamesModule = onlinePlayer.getModule(SummerGamesModule.class);
            gamesModule.receiveSuccess(backMsg.getInfo());
        }

        //被同意日志
        SummerGamesConfig summerGamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        if(summerGamesConfig != null) {
            SummerGamesProto.SummerGamesExchangeInfoTemp info = backMsg.getInfo();
            AutoLogMgr.add(new LogSummerGamesExchange(
                    summerGamesConfig.getActivityId(),
                    info.getPlayerId(),
                    info.getId(),
                    4, //被同意
                    info.getAppointUserId(),  //对方
                    info.getProvideId(),   //我方提供
                    info.getNeedId(),    //对方提供
                    info.getType()
            ));
        }
    }
}
